package api

import (
	context2 "context"
	sentinel "github.com/alibaba/sentinel-golang/api"
	"github.com/alibaba/sentinel-golang/core/base"
	"github.com/gin-gonic/gin"
	"github.com/golang/protobuf/ptypes/empty"
	"kuangstudy-mall/apis/goods-web/microservices"
	"kuangstudy-mall/apis/goods-web/models/context"
	"kuangstudy-mall/apis/goods-web/proto"
	"kuangstudy-mall/apis/goods-web/validation"
	"strconv"
)

type GoodsApi struct {
	BaseApi
}

/*
*
商品广告查询
*/
func (api *GoodsApi) FindBannerList(ctx *gin.Context) {
	var bannerContext context.BannerContext
	err := ctx.ShouldBindJSON(&bannerContext)
	if err != nil {
		api.FailCodeMsgAny(603, validation.GetErrors(err, bannerContext), ctx)
		return
	}

	// 开始远程服务调用
	bannerInfoRequest := proto.BannerInfoRequest{Stype: bannerContext.Stype}
	list, err := microservices.GetNacosGoodsServiceRpcClient().FindBannerList(context2.WithValue(context2.Background(), "ginContext", ctx), &bannerInfoRequest)
	if err != nil {
		api.handlerGrpcError(err, ctx)
		return
	}
	api.OK(list, ctx)
}

/*
*
分类首页接口查询
*/
func (api *GoodsApi) FindCategoryListToIndex(ctx *gin.Context) {
	// 开始远程服务调用
	categoryInfoRequest := proto.CategoryInfoRequest{IsIndex: 1}
	list, err := microservices.GetNacosGoodsServiceRpcClient().FindCategoryListToIndex(context2.WithValue(context2.Background(), "ginContext", ctx), &categoryInfoRequest)
	if err != nil {
		api.handlerGrpcError(err, ctx)
		return
	}
	api.OK(list, ctx)
}

/*
*
查询分类列表
*/
func (api *GoodsApi) FindCategoryList(ctx *gin.Context) {
	// 开始远程服务调用
	list, err := microservices.GetNacosGoodsServiceRpcClient().FindCategoryList(context2.WithValue(context2.Background(), "ginContext", ctx), &empty.Empty{})
	if err != nil {
		api.handlerGrpcError(err, ctx)
		return
	}
	api.OK(list, ctx)
}

/*
*
商品查询
*/
func (api *GoodsApi) FindGoodsList(ctx *gin.Context) {
	var goodsContext context.GoodsContext
	err := ctx.ShouldBindJSON(&goodsContext)
	if err != nil {
		api.FailCodeMsgAny(603, validation.GetErrors(err, goodsContext), ctx)
		return
	}

	// 开始远程服务调用
	bannerInfoRequest := proto.GoodsFilterRequest{
		PageNo:     goodsContext.PageNo,
		PageSize:   goodsContext.PageSize,
		Keyword:    goodsContext.Keyword,
		IsHot:      goodsContext.IsHot,
		IsNew:      goodsContext.IsNew,
		CategoryId: goodsContext.CategoryId,
		MinPrice:   goodsContext.MinPrice,
		MaxPrice:   goodsContext.MaxPrice,
	}

	entry, blockError := sentinel.Entry("goods_list", sentinel.WithTrafficType(base.Inbound))
	if blockError != nil {
		// 意味着被限流了
		api.FailCodeMsgAny(606, "请求过于频繁，请稍后再试!", ctx)
		return
	}
	entry.Exit()
	list, err := microservices.GetNacosGoodsServiceRpcClient().FindGoodsList(context2.WithValue(context2.Background(), "ginContext", ctx), &bannerInfoRequest)
	if err != nil {
		api.handlerGrpcError(err, ctx)
		return
	}
	api.OK(list, ctx)
}

/*
*
商品明细查询
*/
func (api *GoodsApi) GetGoodsDetail(ctx *gin.Context) {
	entry, blockError := sentinel.Entry("goods_list", sentinel.WithTrafficType(base.Inbound))
	if blockError != nil {
		// 意味着被限流了
		api.FailCodeMsgAny(606, "请求过于频繁，请稍后再试!", ctx)
		return
	}
	entry.Exit()

	goodsId := ctx.Param(":id")
	gid, _ := strconv.ParseUint(goodsId, 10, 64)
	request := proto.GoodsInfoRequest{Id: gid}
	detail, err := microservices.GetNacosGoodsServiceRpcClient().GetGoodsDetail(context2.WithValue(context2.Background(), "ginContext", ctx), &request)
	if err != nil {
		api.handlerGrpcError(err, ctx)
		return
	}
	api.OK(detail, ctx)
}
